//
//  CZVisitor.swift
//  Weibo
//
//  Created by 董松 on 16/2/26.
//  Copyright © 2016年 董松. All rights reserved.
//

import UIKit
import SnapKit

/*
1.定义协议用protocol
2.协议继承自nsobjectprotocol
3.用weak来修饰代理属性
*/

protocol CZVisitorDelegate: NSObjectProtocol{
    
    //定义方法
    func didLogin()
    func didRegister()
}

class CZVisitor: UIView {

    //设置代理属性
    weak var delegate: CZVisitorDelegate?
    
    //第一步：重写构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder){
        fatalError("init(coder:) has not been implemented")
    }
    
    //第二步：创建一个私有方法，来添加视图和设置约束
    private func setupUI(){
        
        //添加控件
        addSubview(homeIconImageView)
        //添加遮罩视图
        addSubview(maskImageView)
        
        addSubview(iconImageView)

        addSubview(messageLabel)

        addSubview(registerButton)

        addSubview(loginButton)
        
        //添加约束
        //转动动画居中对齐
        homeIconImageView.snp_makeConstraints { (make) -> Void in make.center.equalTo(self.snp_center)
        }
        iconImageView.snp_makeConstraints { (make) -> Void in make.center.equalTo(self.snp_center)
            
        }
        //添加提示label
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(homeIconImageView.snp_bottom).offset(10)
            make.centerX.equalTo(self.snp_centerX)
            
            make.width.equalTo(216)
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(messageLabel.snp_left)
            
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            make.width.equalTo(100)
            make.height.equalTo(30)
            
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(messageLabel.snp_right)
            make.top.equalTo(messageLabel.snp_bottom).offset(10)
            
            make.width.equalTo(100)
            make.height.equalTo(30)
        }

        
        //添加遮罩视图的约束
        maskImageView.snp_makeConstraints { (make) -> Void in make.center.equalTo(self.snp_center)
        }

    }
    
    //添加动画
    func startAnimation(){
        //千万不要输错了！
        let animation =  CABasicAnimation(keyPath: "transform.rotation")
        
        animation.toValue = 2 * M_2_PI
        
        animation.repeatCount = MAXFLOAT
        
        animation.duration = 5
        
        //切换视图的时候，保证动画不要被移除
        animation.removedOnCompletion = false
        
        //添加到
        homeIconImageView.layer.addAnimation(animation, forKey: nil)
    }
    
    //对外提供一个方法
    func setInfo(message: String,image: String,animation:Bool){
        messageLabel.text = message
        iconImageView.image = UIImage(named: image)
        
        //如果有动画我们就让他显示，并且执行
        if animation {
            
            homeIconImageView.hidden = false
            startAnimation()
        }else{
            homeIconImageView.hidden = true
            //可以移除
        }
    }
    
    //MARK:--按钮点击方法
    func login(){
        
        delegate?.didLogin()
    }
    
    func register(){
        
        delegate?.didRegister()
    }
    
    //第三步：懒加载
    //首页动画图片
    private lazy var homeIconImageView:UIImageView = {
        let img = UIImageView()
        img.image = UIImage(named: "visitordiscover_feed_image_smallicon")
        
        return img
    }()
    
    //通用的图片
    private lazy var iconImageView: UIImageView = {
        let img = UIImageView()
        
        img.image = UIImage(named: "visitordiscover_feed_image_house")
        
        return img
    }()
    
    //提示语
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        //设置属性
        label.font = UIFont.systemFontOfSize(14)
        label.textColor = UIColor.darkGrayColor()
        
        label.numberOfLines = 0
        label.textAlignment = .Center
        label.text = "关注一些人，回这里看看有什么惊喜"
        return label
    }()
    
    //注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "register", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
        
    }()
    
    //登录
    private lazy var loginButton: UIButton = {
        let button = UIButton(type: UIButtonType.Custom)
        
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        
        button.setTitle("登录", forState: UIControlState.Normal)
        
        button.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    //遮罩视图
    
    private lazy var maskImageView: UIImageView = {
        let img = UIImageView()
        
        
        img.image = UIImage(named: "visitordiscover_feed_mask_smallicon")
        return img
    }()

}
